package com.browser;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.transition.Transition;
import com.helper.FileHelper;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.touchgallery.TouchView.TouchImageView;

import java.io.File;

/**
 * Created by fudong2 on 2015/8/5.
 */
public class DetailPicItemView extends FrameLayout {
    private String curOriginalImageType = "";
    Context mContext;
    public PhotoView photoView;
    ProgressBar progressBar;
//    DisplayImageOptions mOptions = new DisplayImageOptions.Builder()
//            .cacheInMemory(true)
//            .cacheOnDisk(true)
//            .imageScaleType(ImageScaleType.NONE)
//            .considerExifParams(true)
//            .bitmapConfig(Bitmap.Config.ARGB_8888)
//            .build();


    public DetailPicItemView(Context context) {
        super(context);
    }

    public DetailPicItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public DetailPicItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public DetailPicItemView(Context context,boolean isInit,String curOriginalImageType) {
        super(context);
        this.curOriginalImageType = curOriginalImageType;
        if(isInit)
            initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.detail_pic_item, this);
        photoView = (PhotoView) findViewById(R.id.photo_item);
//        photoView.setOnClickListener(new PhotoView.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (mContext instanceof Activity)
//                    ((Activity) mContext).finish();
////            }
//        });
        photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener(){
            @Override
            public void onViewTap(View view, float x, float y){
                if (mContext instanceof Activity)
                    ((Activity) mContext).finish();
            }
        });

        progressBar = (ProgressBar) findViewById(R.id.photo_progress_bar);
    }


    public void update(final String pic) {
        if (null != pic) {
            this.setVisibility(VISIBLE);
//            if (StaticInfo.windowWidth > 0) {
//                pic.initRatio();
//                float hw = (float) StaticInfo.windowHeight / StaticInfo.windowWidth;
//                if (pic.ratio >= hw) {
//                    photoView.setScaleType(ImageView.ScaleType.FIT_START);
//                } else {
//                    photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//                }
//            } else {
//                photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//            }
            loadImage(pic);
        } else {
            this.setVisibility(GONE);
        }

    }
/*
 */
    private void loadImage(final String pic) {
        RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.ic_default_picture)
                .fitCenter().error(R.drawable.ic_default_picture)
                .fallback(R.drawable.ic_default_picture);
        if (FileHelper.isFileExists(pic)) {
//            Picasso.get().load(new File(pic.getPicUrl())).placeholder(R.drawable.ic_default_picture).error(R.drawable.ic_default_picture).into(mImageTarget);
            Glide.with(getContext()).asBitmap().load(new File(pic))
                    .apply(lOptions)
                    .listener(mRequestListener)
                    .into(photoView);
        }else if(pic != null && pic.length() > 0){
            Glide.with(getContext()).asBitmap().load(pic)
                    .apply(lOptions)
                    .listener(mRequestListener)
                    .into(photoView);
        }
        else {

            photoView.setImageResource(R.drawable.ic_default_picture);
        }
    }


    private RequestListener<Bitmap> mRequestListener = new RequestListener<Bitmap>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, com.bumptech.glide.request.target.Target<Bitmap> target, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false;
        }

        @Override
        public boolean onResourceReady(Bitmap resource, Object model, com.bumptech.glide.request.target.Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false;
        }
    };



    private Target mImageTarget = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            photoView.setImageBitmap(bitmap);
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onBitmapFailed(Exception pE, Drawable errorDrawable) {
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            photoView.setImageDrawable(errorDrawable);
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            progressBar.setVisibility(View.VISIBLE);
        }
    };

//    private void loadMiddleImage(final JsonPic pic,final String finalImageUrl) {
//        ImageLoader.getInstance().displayImage(finalImageUrl, photoView, mOptions, new ImageLoadingListener() {
//                    @Override
//                    public void onLoadingStarted(String imageUri, View view) {
//                    }
//                    @Override
//                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//                    }
//                    @Override
//                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//                        Logs.e("middle over");
//                        loadOriginalImage(pic, pic.getOriginal_pic());
//                    }
//                    @Override
//                    public void onLoadingCancelled(String imageUri, View view) {
//                    }
//                },
//                new ImageLoadingProgressListener() {
//                    @Override
//                    public void onProgressUpdate(String imageUri, View view, int current, int total) {
//                    }
//                });
//    }
//
//    private void loadOriginalImage(final JsonPic pic, final String finalImageUrl) {
//        ImageLoader.getInstance().displayImage(finalImageUrl, photoView, mOptions, new ImageLoadingListener() {
//                    @Override
//                    public void onLoadingStarted(String imageUri, View view) {
//                        progressBar.setVisibility(VISIBLE);
//                    }
//
//                    @Override
//                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//                        progressBar.setVisibility(GONE);
//                        switch (failReason.getType()) {
//                            case OUT_OF_MEMORY:
//                                ImageLoader.getInstance().displayImage(finalImageUrl,
//                                        photoView, new DisplayImageOptions.Builder()
//                                                .cacheInMemory(true)
//                                                .cacheOnDisk(true)
//                                                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
//                                                .considerExifParams(true)
//                                                .bitmapConfig(Bitmap.Config.RGB_565)
//                                                .build());
//                                break;
//                            default:
//                                break;
//                        }
//                    }
//
//                    @Override
//                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//                        progressBar.setVisibility(GONE);
//                        new Handler().postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                new TagViewLayout(mContext, DetailPicItemView.this, pic).setPicTags();
//                            }
//                        }, 200);// 打开定时器，执行操作
//                    }
//
//                    @Override
//                    public void onLoadingCancelled(String imageUri, View view) {
//                        progressBar.setVisibility(GONE);
//                    }
//                },
//                new ImageLoadingProgressListener() {
//                    @Override
//                    public void onProgressUpdate(String imageUri, View view, int current, int total) {
//                        if (current < total && total > 0) {
//                            progressBar.setProgress(100 * current / total);
//                        }
//                    }
//                });
//    }

    public void resetMatrix() {
//        if (null != photoView)
//            photoView.resetMatrix();
    }
    public void addTagsView(){

    }
}
